/*
 * Copyright 2025 RestfulToolX Contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.welson.restfultoolx.model;

import com.intellij.psi.PsiMethod;
import java.util.List;
import java.util.Objects;

/**
 * RESTful API信息模型
 * 存储Controller方法的详细信息
 */
public class ApiInfo {
    private String id;
    private String httpMethod;     // GET, POST, PUT, DELETE等
    private String path;           // 接口路径
    private String methodName;     // Java方法名
    private String className;      // Controller类名
    private String packageName;    // 包名
    private PsiMethod psiMethod;   // PSI方法引用
    private List<ParamInfo> parameters; // 参数列表
    private String returnType;     // 返回类型
    private List<String> annotations; // 注解信息
    private long lastModified;     // 最后修改时间
    private String filePath;       // 文件路径

    public ApiInfo() {
    }

    public ApiInfo(String httpMethod, String path, String methodName, String className) {
        this.httpMethod = httpMethod;
        this.path = path;
        this.methodName = methodName;
        this.className = className;
        this.id = generateId();
    }

    private String generateId() {
        return className + "#" + methodName + "#" + httpMethod + "#" + path;
    }

    // Getters and Setters
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getHttpMethod() {
        return httpMethod;
    }

    public void setHttpMethod(String httpMethod) {
        this.httpMethod = httpMethod;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public PsiMethod getPsiMethod() {
        return psiMethod;
    }

    public void setPsiMethod(PsiMethod psiMethod) {
        this.psiMethod = psiMethod;
    }

    public List<ParamInfo> getParameters() {
        return parameters;
    }

    public void setParameters(List<ParamInfo> parameters) {
        this.parameters = parameters;
    }

    public String getReturnType() {
        return returnType;
    }

    public void setReturnType(String returnType) {
        this.returnType = returnType;
    }

    public List<String> getAnnotations() {
        return annotations;
    }

    public void setAnnotations(List<String> annotations) {
        this.annotations = annotations;
    }

    public long getLastModified() {
        return lastModified;
    }

    public void setLastModified(long lastModified) {
        this.lastModified = lastModified;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    /**
     * 获取完整的API描述
     */
    public String getFullDescription() {
        return httpMethod + " " + path + " (" + className + "." + methodName + ")";
    }

    /**
     * 获取简短的类名（不包含包名）
     */
    public String getSimpleClassName() {
        if (className == null) {
            return "";
        }
        int lastDot = className.lastIndexOf('.');
        return lastDot >= 0 ? className.substring(lastDot + 1) : className;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ApiInfo apiInfo = (ApiInfo) o;
        return Objects.equals(id, apiInfo.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return "ApiInfo{" +
                "httpMethod='" + httpMethod + '\'' +
                ", path='" + path + '\'' +
                ", methodName='" + methodName + '\'' +
                ", className='" + className + '\'' +
                '}';
    }
}